home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / WindowsTextFieldUI.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  143 lines

  1. /*
  2.  * @(#)WindowsTextFieldUI.java    1.7 98/03/05
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.windows;
  22.  
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import com.sun.java.swing.plaf.*;
  26. import com.sun.java.swing.plaf.basic.BasicTextFieldUI;
  27. import com.sun.java.swing.text.*;
  28. import com.sun.java.swing.*;
  29. import com.sun.java.swing.plaf.UIResource;
  30.  
  31.  
  32.  
  33. /**
  34.  * Provides the Windows look and feel for a text field.  This 
  35.  * is basically the following customizations to the default
  36.  * look-and-feel.
  37.  * <ul>
  38.  * <li>The border is beveled (using the standard control color).
  39.  * <li>The background is white by default.
  40.  * <li>The highlight color is a dark color, blue by default.
  41.  * <li>The foreground color is high contrast in the selected
  42.  *  area, white by default.  The unselected foreground is black.
  43.  * <li>The cursor blinks at about 1/2 second intervals.
  44.  * <li>The entire value is selected when focus is gained.
  45.  * <li>Shift-left-arrow and shift-right-arrow extend selection
  46.  * <li>Cntrl-left-arrow and cntrl-right-arrow act like home and 
  47.  *   end respectively.
  48.  * </ul>
  49.  * <p>
  50.  * Warning: serialized objects of this class will not be compatible with
  51.  * future swing releases.  The current serialization support is appropriate
  52.  * for short term storage or RMI between Swing1.0 applications.  It will
  53.  * not be possible to load serialized Swing1.0 objects with future releases
  54.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  55.  * baseline for the serialized form of Swing objects.
  56.  *
  57.  * @author  Timothy Prinzing
  58.  * @version 1.7 03/05/98
  59.  */
  60. public class WindowsTextFieldUI extends BasicTextFieldUI
  61. {
  62.     /**
  63.      * Creates a UI for a JTextField.
  64.      *
  65.      * @param c the text field
  66.      * @return the UI
  67.      */
  68.     public static ComponentUI createUI(JComponent c) {
  69.         return new WindowsTextFieldUI();
  70.     }
  71.  
  72.     /**
  73.      * Creates the caret for a field.
  74.      *
  75.      * @return the caret
  76.      */
  77.     protected Caret createCaret() {
  78.     return new WindowsFieldCaret();
  79.     }
  80.  
  81.     /**
  82.      * WindowsFieldCaret has different scrolling behavior than
  83.      * DefaultCaret, selects the field when focus enters it, and
  84.      * deselects the field when focus leaves.
  85.      */
  86.     class WindowsFieldCaret extends DefaultCaret implements UIResource {
  87.  
  88.     public WindowsFieldCaret() {
  89.         super();
  90.     }
  91.  
  92.     /**
  93.      * Called when the component containing the caret gains
  94.      * focus.  This is implemented to set the caret to visible
  95.      * if the component is editable, and sets the selection
  96.      * to visible.
  97.      *
  98.      * @param e the focus event
  99.      * @see FocusListener#focusGained
  100.      */
  101.         public void focusGained(FocusEvent e) {
  102.         super.focusGained(e);
  103.         JTextComponent c = getComponent();
  104.         Document doc = c.getDocument();
  105.         setDot(0);
  106.         moveDot(doc.getLength());
  107.     }
  108.  
  109.     /**
  110.      * Called when the component containing the caret loses
  111.      * focus.  This is implemented to set the caret to visibility
  112.      * to false, and to set the selection visibility to false.
  113.      *
  114.      * @param e the focus event
  115.      * @see FocusListener#focusLost
  116.      */
  117.         public void focusLost(FocusEvent e) {
  118.         setDot(getDot());
  119.         setVisible(false);
  120.     }
  121.  
  122.     /**
  123.      * Adjusts the visibility of the caret according to
  124.      * the windows feel which seems to be to move the
  125.      * caret out into the field by about a quarter of
  126.      * a field length if not visible.
  127.      */
  128.     protected void adjustVisibility(Rectangle r) {
  129.         JTextField field = (JTextField) getComponent();
  130.         BoundedRangeModel vis = field.getHorizontalVisibility();
  131.         int x = r.x + vis.getValue();
  132.         int quarterSpan = vis.getExtent() / 4;
  133.         if (x < vis.getValue()) {
  134.         vis.setValue(x - quarterSpan);
  135.         } else if (x > vis.getValue() + vis.getExtent()) {
  136.         vis.setValue(x - (3 * quarterSpan));
  137.         }
  138.     }
  139.     }
  140.  
  141. }
  142.  
  143.